/*
 * MIT License
 *
 * Copyright (c) 2020 wen.gu <454727014@qq.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/***************************************************************************
 * Name: sock_session.h
 *
 * Purpose: socket session base API define and implementation
 *
 * Developer:
 *   wen.gu , 2023-11-01
 *
 * TODO:
 *
 ***************************************************************************/

#ifndef __XBUS_COM_INTERNAL_SOCK_SESSION_H__
#define __XBUS_COM_INTERNAL_SOCK_SESSION_H__

#include "xbus/com/xsession.h"

#include "xbus/core/xqueue.h"
#include "xbus/core/xsocket.h"
#include "xbus/core/xthread.h"





namespace xbus::com::internal {

class SockSession: public XSession {
public:
    using XSocketId = core::XSocketId;
    using XResultCode = core::XResultCode;
    using IoWorker = std::unique_ptr<core::XThread>;
public:
    virtual ~SockSession();
    SockSession(XSocketId sock_id, const std::string& session_name);

public:
    virtual XResultCode start(const XSessionStartParam& param);
    virtual XResultCode stop();

public:
    virtual XResultCode reset(XSocketId sock_id); /**  first clean send and receive queue, then update sock id */

public:
    virtual XResultCode send(XMessageSp msg);
    virtual core::XResult<XMessageSp> recv(int32_t timeout_ms = core::WaitType::WaitForever);

protected:
    virtual void onSendWorker();
    virtual void onRecvWorker();
protected:
    bool is_running_ = false;
    IoWorker recv_worker_ = nullptr;
    IoWorker send_worker_ = nullptr;
    XMessageQueueSp recv_queue_ = nullptr;
    XMessageQueueSp send_queue_ = nullptr;
};

} //namespace xbus::com::internal


#endif /** !__XBUS_COM_INTERNAL_SOCK_SESSION_H__ */